package com.bawei.module_repair.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_repair.R
import com.bawei.module_repair.model.PhotoItem
import com.bumptech.glide.Glide



class PhotoAdapter(
    private val itemList: MutableList<PhotoItem>,
    private val photoClickListener: OnPhotoClickListener,
    private val addPhotoClickListener: OnAddPhotoClickListener,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val PHOTO_TYPE = 0
    private val ADD_TYPE = 1

    interface OnPhotoClickListener {
        fun onPhotoClick(position: Int, photoItem: PhotoItem.Photo)
    }

    interface OnAddPhotoClickListener {
        fun onAddPhotoClick()
    }

    inner class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val photoImage: ImageView = itemView.findViewById(R.id.icon)
    }

    inner class AddViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val addImage: ImageView = itemView.findViewById(R.id.icon)
    }

    override fun getItemViewType(position: Int): Int {
        return if (itemList[position] is PhotoItem.AddPhoto) ADD_TYPE else PHOTO_TYPE
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            PHOTO_TYPE -> {
                val view = LayoutInflater.from(parent.context).inflate(
                    R.layout.image,
                    parent,
                    false
                )
                view.findViewById<ImageView>(R.id.icon).scaleType = ImageView.ScaleType.CENTER_CROP
                PhotoViewHolder(view)
            }

            ADD_TYPE -> {
                val view = LayoutInflater.from(parent.context).inflate(
                    R.layout.image,
                    parent,
                    false
                )
                view.findViewById<ImageView>(R.id.icon).setImageResource(R.drawable.additional)
                AddViewHolder(view)
            }
            else -> throw IllegalArgumentException("未知视图类型")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is PhotoViewHolder -> {
                val photoItem = itemList[position] as PhotoItem.Photo
                Glide.with(holder.itemView)
                    .load(photoItem.path)
                    .into(holder.photoImage)
                holder.photoImage.setOnClickListener {
                    photoClickListener.onPhotoClick(position, photoItem)
                }
            }

            is AddViewHolder -> {
                holder.addImage.setOnClickListener {
                    // 打开图片选择逻辑
                    // 此处省略具体实现，可参考前文的权限获取和相册打开代码
                    addPhotoClickListener.onAddPhotoClick()
                }
            }
        }
    }

    override fun getItemCount(): Int = itemList.size

    fun addPhoto(photoPath: String) {
        val addIndex = itemList.indexOfFirst { it is PhotoItem.AddPhoto }
        itemList.add(addIndex, PhotoItem.Photo(photoPath))
        notifyItemInserted(addIndex)
    }
}